package com.platform.controller;

import com.alibaba.fastjson.JSONObject;
import com.platform.annotation.SysLog;
import com.platform.entity.SysMerchantEntity;
import com.platform.service.SysMerchantService;
import com.platform.service.SysUserRoleService;
import com.platform.utils.*;
import com.platform.validator.Assert;
import com.platform.validator.ValidatorUtils;
import com.platform.validator.group.AddGroup;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 系统用户Controller
 *
 * @author liweihai
 * @email liweihai12345@sina.com
 * @date 2019-04-24 11:30:54
 */
@RestController
@RequestMapping("/sys/merchant")
public class SysMerchantController extends AbstractController {
    @Autowired
    private SysMerchantService sysMerchantService;
    @Autowired
    private SysUserRoleService sysUserRoleService;
    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sysmerchant:list")
    public R list(@RequestParam Map<String, Object> params) {
//         只有超级管理员，才能查看所有管理员列表
        if (getUserId() != Constant.SUPER_ADMIN) {
            logger.info("userId : "+getUser());
            params.put("createUserId", getUserId());
        }
        //查询列表数据
        Query query = new Query(params);

        List<SysMerchantEntity> sysMerchantList = sysMerchantService.queryList(query);
        logger.info(" list : "+ JSONObject.toJSONString(sysMerchantList));
        int total = sysMerchantService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(sysMerchantList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{userId}")
    @RequiresPermissions("sysmerchant:info")
    public R info(@PathVariable("userId") Long userId) {
        SysMerchantEntity sysMerchant = sysMerchantService.queryObject(userId);

        //获取用户所属的角色列表
        List<Long> roleIdList = sysUserRoleService.queryRoleIdList(userId);
        sysMerchant.setRoleIdList(roleIdList);
        return R.ok().put("user", sysMerchant);
    }

    /**
     * 修改登录用户密码
     */
    @SysLog("修改密码")
    @RequestMapping("/password")
    public R password(String password, String newPassword) {
       /* if(ResourceUtil.getConfigByName("sys.demo").equals("1")){
            throw new RRException("演示环境无法修改密码！");
        }*/
        Assert.isBlank(newPassword, "新密码不为能空");

        //sha256加密
        password = new Sha256Hash(password).toHex();
        //sha256加密
        newPassword = new Sha256Hash(newPassword).toHex();

        //更新密码
        int count = sysMerchantService.updatePassword(getUserId(), password, newPassword);
        if (count == 0) {
            return R.error("原密码不正确");
        }

        //退出
        ShiroUtils.logout();

        return R.ok();
    }
    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sysmerchant:save")
    public R save(@RequestBody SysMerchantEntity sysMerchant) {
        ValidatorUtils.validateEntity(sysMerchant, AddGroup.class);

        sysMerchant.setCreateUserId(getUserId());
        sysMerchantService.save(sysMerchant);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sysmerchant:update")
    public R update(@RequestBody SysMerchantEntity sysMerchant) {
        sysMerchantService.update(sysMerchant);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sysmerchant:delete")
    public R delete(@RequestBody Long[] userIds) {
        sysMerchantService.deleteBatch(userIds);
        sysUserRoleService.deleteBatch(userIds);
        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<SysMerchantEntity> list = sysMerchantService.queryList(params);

        return R.ok().put("list", list);
    }

    @RequestMapping("/queryMerchant")
    public R queryAllSysMerchant(){
        List<SysMerchantEntity> queryList = sysMerchantService.querySysmerchant();
        return R.ok().put("list",queryList);
    }
}
